package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.College;
import br.com.gaacorp.sai.webapp.action.BasePage;
import br.com.gaacorp.sai.service.CollegeManager;


public class CollegeForm extends BasePage implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 6225324418024766046L;
	private CollegeManager collegeManager;
    private College college = new College();
    private Long collegeId;

    public void setCollegeManager(CollegeManager collegeManager) {
        this.collegeManager = collegeManager;
    }

    public College getCollege() {
        return college;
    }

    public void setCollege(College college) {
        this.college = college;
    }

    public void setCollegeId(Long collegeId) {
        this.collegeId = collegeId;
    }

    public String delete() {
        collegeManager.remove(college.getCollegeId());
        addMessage("college.deleted");

        return "collegeList";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (collegeId != null && collegeId != 0) {
            college = collegeManager.get(collegeId);
        } else {
            college = new College();
        }

        return "collegeForm";
    }

    public String save() {
        boolean isNew = (college.getCollegeId() == null || college.getCollegeId() == 0);
        collegeManager.save(college);

        String key = (isNew) ? "college.added" : "college.updated";
        addMessage(key);

        if (isNew) {
            return "collegeList";
        } else {
            return "collegeForm";
        }
    }
} 